/*
 * Powered By 
 * Since 2010 - 2016
 */

package com.rf.sys.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.rf.sys.dao.DictionaryMapper;
import com.rf.sys.dao.SqlAdapterMapper;
import com.rf.sys.model.Dictionary;
import com.rf.sys.model.DictionaryExample;
import com.rf.sys.service.DictionaryService;
import com.rf.sys.utils.ConfigUtils;
import com.rf.sys.utils.PageUtils;
import com.rf.sys.utils.UuidUtil;

@Service("dictionaryService")
public class DictionaryServiceImpl implements DictionaryService {
	
	@Autowired
   	private SqlAdapterMapper sqlAdapterMapper;//注入dao
   	@Autowired
   	private DictionaryMapper dictionaryMapper;//注入dao

   	/**
	 * 根据id获取数据
	 * @param dictionaryId
	 * @return Dictionary
	 */
   	@Override
	public Dictionary getDictionaryByID(String id) throws Exception{
		// TODO Auto-generated method stub
		return dictionaryMapper.selectByPrimaryKey(id);
	}
	
	/**
	 * 根据Dictionary获取数据
	 * @param Dictionary
	 * @return List<Dictionary>
	 */
	@Override
	public List<Dictionary> getDictionary(Dictionary dictionary) throws Exception{
		// TODO Auto-generated method stub
		DictionaryExample example = new DictionaryExample();
		com.rf.sys.model.DictionaryExample.Criteria criteria= example.createCriteria();
		if(dictionary.getClassCode() != null && !dictionary.getClassCode().equals("")){
			criteria.andClassCodeEqualTo(dictionary.getClassCode());
		}
		if(dictionary.getDataCode() != null && !dictionary.getDataCode().equals("")){
			criteria.andDataCodeEqualTo(dictionary.getDataCode());
		}
		criteria.andStateNotEqualTo(ConfigUtils.DELSTATE);//不查询已删除的
		example.setOrderByClause("data_order ");
		List<Dictionary> list = dictionaryMapper.selectByExample(example);
		return list;
	}
	
	/**
	 * 获取所有数据
	 * @param 
	 * @return List<Dictionary>
	 */
	@Override
	public List<Dictionary> getDictionary() throws Exception{
		// TODO Auto-generated method stub
		DictionaryExample example = new DictionaryExample();
		return dictionaryMapper.selectByExample(example);
	}
	
	/**
	 * 根据datakey查询内容，不包含datavalue
	 * @param datakey
	 * @param datavalue
	 * @return
	 */
	@Override
	public List<Dictionary> getDictionaryByDatakey(String classCode,List<String> exceptDataCode) {
		// TODO Auto-generated method stub
		DictionaryExample example = new DictionaryExample();
		com.rf.sys.model.DictionaryExample.Criteria criteria= example.createCriteria();
		criteria
			.andClassCodeEqualTo(classCode)
			.andStateEqualTo(ConfigUtils.ENABLESTATE);;//状态等于启用
		if(exceptDataCode != null && exceptDataCode.size()>0){
			criteria.andDataCodeNotIn(exceptDataCode);
		}
		example.setOrderByClause("data_order ");
		return dictionaryMapper.selectByExample(example);
	}
   	
	/**
	 * 保存信息
	 * @param Dictionary
	 * @return
	 */
	@Override
	public void addDictionary(Dictionary dictionary) throws Exception{
		// TODO Auto-generated method stub
		dictionary.setId(UuidUtil.get32UUID());
		dictionaryMapper.insert(dictionary);
	}

	/**
	 * 修改数据
	 * @param Dictionary
	 * @param
	 */
	@Override
	public void updateDictionary(Dictionary dictionary) throws Exception{
		// TODO Auto-generated method stub
		dictionaryMapper.updateByPrimaryKeySelective(dictionary);
	}
	
	/**
	 * 删除数据
	 * @param dictionaryId
	 * @param
	 */
	@Override
	public void deleteDictionary(String id) throws Exception{
		// TODO Auto-generated method stub
		dictionaryMapper.deleteByPrimaryKey(id);
	}
	
	/**
	 * 删除数据
	 * @param ids
	 * @param
	 */
	@Override
	public void deleteDictionaryState(String ids) throws Exception{
		// TODO Auto-generated method stub
		String sql = "update sys_dictionary set state = "+ConfigUtils.DELSTATE +" where id in("+ids+")";
		sqlAdapterMapper.updateSQL(sql);
	}

	/**
	 * 获取数据分页
	 * @param Dictionary
	 * @param limit
	 * @param offset
	 * @return Map
	 */
	@Override
	public Map getDictionaryPage(Dictionary dictionary,PageUtils pageUtils) throws Exception{
		DictionaryExample example = new DictionaryExample();
		com.rf.sys.model.DictionaryExample.Criteria criteria= example.createCriteria();
		if(dictionary.getClassCode()!=null && !dictionary.getClassCode().equals("")){
			criteria.andClassCodeLike("%"+dictionary.getClassCode()+"%");
		}
		if(dictionary.getClassName()!=null && !dictionary.getClassName().equals("")){
			criteria.andClassNameLike("%"+dictionary.getClassName()+"%");
		}
		if(dictionary.getState()!=null && !dictionary.getState().equals("")){
			criteria.andStateEqualTo(dictionary.getState());
		}
		criteria.andStateNotEqualTo(ConfigUtils.DELSTATE);//不等于删除状态
		example.setOrderByClause(" CLASS_CODE desc,DATA_ORDER ");
		//获取第page页，每页共rows条内容，默认查询总数count
		Page p = PageHelper.startPage(pageUtils.getPage(), pageUtils.getRows());
		//下一条执行语句，即为要被转换的语句
		List<Dictionary> list = dictionaryMapper.selectByExample(example);
		Map dateMap = new HashMap();
	    dateMap.put("total", p.getTotal());
	    dateMap.put("rows", list);
		return dateMap;
	}
}

